package com.xxl.controller;

import com.xxl.util.FiledConstant;
import com.xxl.util.UrlConstant;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 登录页
 *
 * @author xxl
 * @date 2023/3/7
 */
@Controller
@RequestMapping(value = "user")
public class LoginController {
    private final String username = "admin";
    private final String password = "admin123";


    @GetMapping("login")
    public String login(@RequestParam("username") String user,
                        @RequestParam("password") String pwd,
                        Model model, HttpSession session) {
        if (user.equals(username) && pwd.equals(password)) {
            session.setAttribute(FiledConstant.USER_INFO, "username=admin,password=admin123");
            //为啥重定向因为，因为默认转发，地址栏带参数不安全
            return "redirect:" + UrlConstant.JUMP_MAIN_PAGE;
        }
        System.out.println("登录失败");
        model.addAttribute("msg", "登录失败");
        return "/page/login";
    }

    /**
     * 退出登录
     * @return String
     * @author xxl
     */
    @GetMapping("out")
    public String signOut(HttpSession session) {
        session.invalidate();
        //退出登录后返回首页这里用index在html用 “/”就代表index页面，不过需要thymeleaf接管,在拦截器中“/”又表示index页面就很sb
        return "index";
    }
}
